package com.ydc.ln.commons.utils;

public class CodeUtil {

}
/*
#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os
import sys

class State(object):
    CODE = 0
    COMMENT = 1
    DOCUMENT = 2

class LanguageSyntax(object):
    """
    语法策略
    """
    def __init__(self, linecomment, blockcomment, document, unless_tokens):
        self.linecomment = linecomment
        self.blockcomment = blockcomment
        self.document = document
        self.unless_tokens = unless_tokens

class Counter(object):   
    """
    代码行数统计器
    """
    def __init__(self, syntax_strategy, source=None):
        self.syntax = syntax_strategy
        self.raw_lines = 0
        self.clean_lines = 0
        self.comment_lines = 0
        self.white_lines = 0
        self.document_lines = 0
        self.unless_lines = 0
        self._state = State.CODE
        if source:
            self.source = source

    def count(self, *counters):
        for counter in counters:
            self.raw_lines += counter.raw_lines
            self.clean_lines += counter.clean_lines
            self.comment_lines += counter.comment_lines
            self.white_lines += counter.white_lines
            self.document_lines += counter.document_lines
            self.unless_lines += counter.unless_lines
    
    @property
    def source(self):
        return self.source
    
    @source.setter
    def source(self, source):
        source = source.replace("\r\n", "\n")
        self._source = source
        self._lines = source.split("\n")
        self.update_count()
    
    def update_count(self):
        self.raw_lines = len(self._lines)
        unless_tokens = self.syntax.unless_tokens
        
        for line in self._lines:
            line = line.strip()
            if self._state is State.CODE:
                if line.startswith(self.syntax.linecomment) or \
                   line.endswith(self.syntax.blockcomment[1]) or \
                   line.endswith(self.syntax.document[1]):
                    self.comment_lines += 1 # 行注释
                elif line.startswith(self.syntax.document[0]):
                    self.document_lines += 1 # 文档注释开始
                    self._state = State.DOCUMENT
                elif line.startswith(self.syntax.blockcomment[0]):
                    self.comment_lines += 1 # 块注释开始
                    self._state = State.COMMENT
                elif len(line):
                    self.white_lines += 1 # 空白行
                elif any((unless in line) for unless in unless_tokens):
                    self.unless_lines +=1 # 无语义行
                else:
                    self.clean_lines += 1 # 干货
            elif self._state is State.COMMENT:
                self.comment_lines += 1
                if line.endswith(self.syntax.blockcomment[1]):
                    self._state = State.CODE # 注释结束
            elif self._state is State.DOCUMENT:
                self.document_lines += 1
                if line.endswith(self.syntax.document[1]):
                    self._state = State.CODE # 文档结束

    def __str__(self):
        output = ["=" * 80,
                  "Language Syntax: %s" % self.syntax,
                  "Lines Count: %d" % self.raw_lines,
                  "Unless Token: %s" % "|".join(self.syntax.unless_tokens),
                  "-" * 80,
                  "Clean Code Lines Count: %d" % self.clean_lines,
                  "Unless Code Lines Count: %d" % self.unless_lines,
                  "Comment Lines Count: %d" % self.comment_lines,
                  "Document Lines Count: %d" % self.document_lines,
                  "Blank Lines Count: %d" % self.white_lines,
                  "=" * 80]
        return "\n".join(output)

result = []
*/
// java_syntax = LanguageSyntax("//", ("/*","*/"), ("/**","*/"), ["import", "package"])
/*

full_counter = Counter(java_syntax)
def count_dir(path, syntax):
    for node_name in os.listdir(path):
        node = os.path.abspath(os.path.join(path,node_name))
        if os.path.isdir(node):
            count_dir(node, syntax) # 递归子目录
        elif os.path.isfile(node):
            fileext = os.path.splitext(node)[-1]
            with open(node, "r") as source_file:
                counter = Counter(syntax, source_file.read())
                full_counter.count(counter) # 合计
                result.append(node)
                result.append(str(counter))
                result.append("")
                result.append("")
    return result

def main(self, source_path, output_path):
    report = count_dir(source_path, java_syntax)
    with open(output_path, "w") as report_file:
        report_file.write("\n".join(report))
        report_file.write("\n"*10)
        report_file.write("SUM FULL\n")
        report_file.write(str(full_counter))

if __name__ == "__main__":
    try:
        main(*sys.argv)
    except TypeError:
        sys.stdout = sys.stderr
        print("Error argument")
        print("Usage: python %s [source path] [output path]" % sys.argv[0])
    except KeyboardInterrupt:
        print("Exit")

        */